package com.example.leo.mycharmingstudent.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.example.leo.mycharmingstudent.R;

import java.util.Locale;

/**
 * Created by Leo on 2020/10/13.
 */

public class PropertyView extends View {
    private static final String TAG = "PropertyView";

    private String mPropertyString = "hello YINGGU";

    private Paint mPaint;

    /**
     * 通过代码创建自定义View的对象，使用这个构造方法
     */
//    public PropertyView(Context context) {
//        super(context);
//    }

    /**
     * 使用布局文件创建的自定义View对象，使用这个构造方法
     */
    public PropertyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PropertyView);
        boolean propertyBoolean = typedArray.getBoolean(R.styleable.PropertyView_property_boolean, false);
        int propertyInt = typedArray.getInt(R.styleable.PropertyView_property_integer, 0);
        float propertyDimen = typedArray.getDimension(R.styleable.PropertyView_property_dimension, 30);
        mPropertyString = typedArray.getString(R.styleable.PropertyView_property_string);
        if (TextUtils.isEmpty(mPropertyString)) {
            mPropertyString = "hello YINGGU";
        }
        //mPropertyString的值为啥是null？参考以下代码
        //mPropertyString第一次赋值为hello YINGGU 第二次赋值为null，
        //so，mPropertyString的值为null
        /*String a = "1";
        a = null;*/
        //此时a的值就是null

        //获取使用到的自定义属性的数量
        //避免string类型变量被赋值为null的一种办法
        /*int indexCount = typedArray.getIndexCount();
        for (int i=0;i<indexCount;i++){
            int index = typedArray.getIndex(i);
            switch (index){
                case R.styleable.PropertyView_property_string:
                    //布局文件中使用了property_string这个属性，
                    //index的值才有可能是R.styleable.PropertyView_property_string id值
                    mPropertyString = typedArray.getString(R.styleable.PropertyView_property_string);
                    break;
            }
        }*/

        String format = String.format(Locale.CHINA,
                "boolean:%s int:%d dimension:%s string:%s",
                propertyBoolean,
                propertyInt,
                propertyDimen,
                mPropertyString);

        Log.e(TAG, format);

        //资源回收
        typedArray.recycle();

        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint();
        //将画笔的颜色设置为红色
        mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    /**
     * 测量控件大小
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 专门画图，绘制图像
     * onDraw方法会执行多次，注意避免创建多余的对象
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, 40, mPaint);

        canvas.drawLine(0,getHeight()/2,getWidth(),getHeight()/2,mPaint);
        canvas.drawLine(getWidth()/2,0,getWidth()/2,getHeight(),mPaint);

        if (!TextUtils.isEmpty(mPropertyString)) {
            mPaint.setTextSize(40);
            canvas.drawText(mPropertyString, 0, getHeight(), mPaint);
        }

    }

    /**
     * 获取触摸事件
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mPropertyString = "888";
        //重新绘制，也就是说，会调用onDraw方法
        invalidate();
        //return true表示触摸事件被自定义控件消耗了
        return true;
    }

    private static final String INSTANCE = "instance";
    private static final String KEY_TEXT = "key_text";

    /**
     * Activity被意外销毁，自定义控件会执行此方法
     * 为了保存数据
     */
    @Override
    protected Parcelable onSaveInstanceState() {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_TEXT, mPropertyString);
        bundle.putParcelable(INSTANCE, super.onSaveInstanceState());
        return bundle;
    }

    /**
     * Activity被重建，自定义控件会执行此方法
     * 为了恢复数据
     */
    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state instanceof Bundle)
        {
            Bundle bundle = (Bundle) state;
            Parcelable parcelable = bundle.getParcelable(INSTANCE);
            super.onRestoreInstanceState(parcelable);
            mPropertyString = bundle.getString(KEY_TEXT);
            return;
        }
        super.onRestoreInstanceState(state);
    }
}
